package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * This associaton class represents relationship between {@link Weapon} and
 * {@link CreatureType} where effectivity of that weapon against particular 
 * creature type is taken into consideration. This relationship is of the 
 * kind "many-to-many".
 * 
 * @author xmiklos1
 */
@Entity
@Table(name = "CREATURE_TYPE_WEAPON")
@AssociationOverrides({
    @AssociationOverride(name = "pk.weapon", joinColumns =
    @JoinColumn(name = "WEAPON_ID")),
    @AssociationOverride(name = "pk.creaturetype", joinColumns =
    @JoinColumn(name = "CREATURETYPE_ID"))
})
public class CreatureTypeWeaponEffectivity implements Serializable {

    /**
     * Key in the association table
     */
    private CreatureTypeWeaponEffectivityId pk = new CreatureTypeWeaponEffectivityId();
    
    /**
     * Effectivity of the unique connection of the creature type and weapon, 
     * measured in percents of the {@link CreatureType#maxHitPoints}.
     */
    private Float effectivity;

    /**
     * Default constructor in which {@link CreatureTypeWeaponEffectivity#pk} is instantiated.
     */
    public CreatureTypeWeaponEffectivity() {
        pk = new CreatureTypeWeaponEffectivityId();
    }

    /**
     * 
     * @return key of the weapon-creatureType relationship
     */
    @EmbeddedId
    public CreatureTypeWeaponEffectivityId getPk() {
        return pk;
    }

    /**
     * 
     * @param pk 
     */
    public void setPk(CreatureTypeWeaponEffectivityId pk) {
        this.pk = pk;
    }

    /**
     * 
     * @return weapon
     */
    @Transient
    public Weapon getWeapon() {
        return getPk().getWeapon();
    }

    /**
     * 
     * @param weapon 
     */
    public void setWeapon(Weapon weapon) {
        getPk().setWeapon(weapon);
    }

    /**
     * 
     * @return creature's type
     */
    @Transient
    public CreatureType getCreatureType() {
        return getPk().getCreaturetype();
    }

    /**
     * 
     * @param creatureType 
     */
    public void setCreatureType(CreatureType creatureType) {
        getPk().setCreaturetype(creatureType);
    }

    /**
     * Gets  effectivity of the {@link Weapon} against 
     * particular {@link CreatureType}.
     * 
     * @return effectivity of a {@link Weapon} against particular {@link CreatureType}
     */
    @Column(name = "EFFECTIVITY", nullable = false)
    public Float getEffectivity() {
        return this.effectivity;
    }

    /**
     * Sets effectivity of a {@link Weapon} against 
     * particular {@link CreatureType}.
     * 
     * @param effectivity of a {@link Weapon} against particular {@link CreatureType}
     */
    public void setEffectivity(Float effectivity) {
        this.effectivity = effectivity;
    }

    @Override
    public String toString() {
        return "CreatureTypeWeaponEffectivity{" + "pk=" + pk + ", effectivity=" + effectivity + '}';
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CreatureTypeWeaponEffectivity that = (CreatureTypeWeaponEffectivity) o;

        if (getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return (getPk() != null ? getPk().hashCode() : 0);
    }
}
